#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

ll n, m, p;
ll f[N];
void init() {
  f[0] = 1;
  rep(i, 1, p) f[i] = f[i - 1] * i % p;
}
ll fastpow(ll b, ll p, ll mod) {
  ll res = 1;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
ll inv(ll x) { return fastpow(x, p - 2, p); }
ll cmb(ll n, ll m, ll p) {
  if (m > n) return 0;
  ll res = f[n] * inv(f[m]) % p * inv(f[n - m]) % p;
  // cout << n << ' ' << m << ' ' << p << ' ' << res << endl;
  return res;
}
ll lucas(ll n, ll m, ll p) {
  if (m > n) return 0;
  if (m == n || m == 0) return 1;
  return lucas(n / p, m / p, p) * cmb(n % p, m % p, p) % p;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> m >> p;
    init();
    cout << lucas(n + m, n, p) << endl;
  }
  return 0;
}